package com.smart.service.impl;

import com.smart.pojo.Produce;
import com.smart.mapper.ProduceMapper;
import com.smart.resp.EchartsData;
import com.smart.resp.ProduceResp;
import com.smart.service.IProduceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-05-14
 */
@Service
public class ProduceServiceImpl extends ServiceImpl<ProduceMapper, Produce> implements IProduceService {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<ProduceResp> queryAll() {
        List<ProduceResp> planList = (List<ProduceResp>) redisTemplate.opsForValue().get("plan");
        System.out.println("Redis缓存查找到的生产计划：" + planList);
        if (planList == null) {
            List<ProduceResp> produceResps = baseMapper.queryAll();
            redisTemplate.opsForValue().set("plan", produceResps);
            return produceResps;
        } else {
            return planList;
        }
    }

    @Override
    public List<EchartsData> getPlanData() {

        return baseMapper.getPlanData();
    }

    @Override
    public boolean saveOrUpdateProduce(Produce produce) {
        redisTemplate.delete("plan");
        if (produce.getId() == null){
            return this.save(produce);
        }else {
            return this.updateById(produce);
        }
    }
}
